package com.duan.goods.controller;


import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.duan.goods.pojo.Brand;
import com.duan.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 品牌表
 */
@RestController
@RequestMapping("/Brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/selectAll")
    @PreAuthorize("hasAnyAuthority('a')")
    public Result<List<Brand>> selectAll(){
//        return new Result<>("查询成功",brandService.selectAll());
        return Result.ok("查询成功",brandService.selectAll());
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Brand brand){
        brandService.insert(brand);
        return Result.ok("新增成功");
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Brand brand){
        brandService.update(brand);
        return Result.ok("修改成功");
    }

    /**
     * 删除品牌
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        brandService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改品牌状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status")
    public Result status(@RequestParam Long id,@RequestParam Integer status){
        if(id == null){
            return Result.fail(StatusCode.ERROR,"查询不到品牌");
        }
        Integer integer=null;
        if(status == null){
            integer=brandService.status(id);
        }else{
            integer=brandService.status(id,status);
        }
        if(integer != 1){
            return Result.fail(StatusCode.ERROR,"品牌状态修改失败");
        }
        return Result.ok(StatusCode.OK,"品牌状态修改成功");
    }

    /**
     * 品牌列表条件查询
     * @param brand
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Brand>> select(@RequestBody Brand brand){
        return Result.ok("查询成功",brandService.selectList(brand));
    }

    /**
     * 品牌分页搜索实现（分页没传给个默认值）
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    public Result findPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList=brandService.findPage(page,size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 品牌名称分页搜索实现
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/pageList")
    public Result pageList(@RequestBody Brand brand,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        Page<Brand> pageList=brandService.pageList(brand,page,size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param categoryName
     * @return
     */
    @GetMapping("/category")
    public Result findListByCategoryName(@RequestParam String categoryName){
        List<Map> brandList = brandService.findListByCategoryName(categoryName);
        return Result.ok("查询成功",brandList);
    }

}
